package com.boarsoft.message.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

public class AmqConsumer implements AmqMsgStub, MessageListener {
	// private static Logger log = LoggerFactory.getLogger(AmqConsumer.class);

	/** */
	protected MessageConsumer consumer;
	/** */
	protected MessageListener listener;
	
	public void close() throws JMSException {
		consumer.close();
		consumer = null;
	}

	@Override
	public void init(Session session, Destination destination) throws JMSException {
		consumer = session.createConsumer(destination);
		consumer.setMessageListener(this);
	}

	@Override
	public void onMessage(Message msg) {
		if (listener != null) {
			listener.onMessage(msg);
		}
	}

	public Message receive() throws Exception {
		return consumer.receive();
	}

	public MessageConsumer getConsumer() {
		return consumer;
	}

	public MessageListener getListener() {
		return listener;
	}

	public void setListener(MessageListener listener) {
		this.listener = listener;
	}
}
